﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using viBook.Common;
using viBook.Models;

namespace viBook.Helpers
{
    public static class Helpers
    {
        public static string BooleanToJavascript(this bool b)
        {
            return b.ToString().ToLower();
        }

        public static string InvertBooleanToJavascript(this bool b)
        {
            if (b)
                return false.ToString().ToLower();
            return true.ToString().ToLower();
        }

        public static ControllerEnum ControllerNameToEnum(string controllerName)
        {
            return (ControllerEnum)Enum.Parse(typeof(ControllerEnum), char.ToUpper(controllerName[0]) + controllerName.Substring(1));
        }

        public static string ConvertDateToString(this DateTime dateTime)
        {
            TimeSpan timeSpan = DateTime.Now.Subtract(dateTime);
            if (timeSpan.Days > 0)
                return dateTime.ToShortDateString();
            if (timeSpan.Hours > 0)
                return timeSpan.Hours + " Hours ago";
            if (timeSpan.Minutes > 0)
            {
                return timeSpan.Minutes + " Minutes ago";
            }
            return "Few seconds ago";
        }

        public static HtmlString ConvertDes(string description)
        {
            if (string.IsNullOrEmpty(description))
                return new HtmlString(string.Empty);
            if (description.Length < Constant.MaxCharToTake)
            {
                var convertDes = new HtmlString(description.ConvertEscapeChars());
                return convertDes;
            }
            string des = description.ConvertEscapeChars().Substring(0, Constant.MaxCharToTake);
            for (int i = Constant.MaxCharToTake; i > 0; i--)
            {
                if (description.Substring(0, i).Equals(string.Empty))
                {
                    des = description.Substring(0, i);
                    break;
                }
            }
            return new HtmlString(des + " ...");
        }

        private static string ConvertEscapeChars(this string description)
        {
            if (string.IsNullOrEmpty(description)) return string.Empty;
            return description
                .Replace("\r\n", @"\r\n")
                .Replace("\'", @"\'")
                .Replace("\''", @"\''")
                .Replace("\r", @"\r")
                .Replace("\t", @"\t")
                .Replace("\b", @"\b")
                .Replace("\f", @"\f")
                .Replace("\n", @"\r\n");
        }

        public static HtmlString ConvertToHtmlString(string text)
        {
            text = text.ConvertEscapeChars();
            if (string.IsNullOrEmpty(text))
                return new HtmlString(string.Empty);

            return new HtmlString(text);
        }

        public static void DeleteFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName) || fileName.Equals(Constant.DefaultBookUrl)) return;
            string fullFileName = AppDomain.CurrentDomain.BaseDirectory + fileName.Replace("/", "\\");
            File.Delete(fullFileName);
        }

        public static void DeleteCropedFile(string imageSource)
        {
            File.Delete(imageSource);
        }

        public static object ListBookToJsonResult(this List<Book> listBooks)
        {
            return new
                       {
                           isSuccess = true,
                           books = listBooks.Select(b => new
                                                             {
                                                                 description = b.Description,
                                                                 id = b.Id,
                                                                 title = b.Title,
                                                                 coverImage = b.CoverUrl,
                                                                 numberOfPages = b.NumberOfPage,
                                                                 numberOfChapters = b.NumberOfChapters,
                                                                 numberOfLikes = b.NumberOfLikes,
                                                                 numberOfViews = b.NumberOfViews,
                                                                 numberOfComments = b.NumberOfComments,
                                                                 uploaderImage = b.UploaderAvartar,
                                                                 uploaderName = b.UploaderName,
                                                                 uploadedBy = b.UploadedBy,
                                                                 uploadedDateString =
                                                             ConvertDateToString(b.UploadedDate)
                                                             })
                       };
        }

        public static object CommentListToJson(this IList<Comment> comments, int currentUserId)
        {
            return new
                       {
                           isSuccess = true,
                           comments = comments.Select(c => new
                                                               {
                                                                   id = c.Id,
                                                                   username = c.User.DisplayName,
                                                                   userImage = c.User.AvatarUrl,
                                                                   content = c.Content,
                                                                   createdDate = ConvertDateToString(c.CreatedDate),
                                                                   like = c.Like,
                                                                   isReported = c.IsReported,
                                                                   isLiked = c.IsLiked,
                                                                   isAuthorized =
                                                               (currentUserId != 0 && currentUserId == c.UserId)
                                                               })
                       };
        }
    }
}